import spawn from "spawndamnit";


export async function add(pathToFile, cwd) {
  const gitCmd = await spawn("git", ["add", pathToFile], { cwd });

  if (gitCmd.code !== 0) {
    console.log(pathToFile, gitCmd.stderr.toString());
  }
  return gitCmd.code === 0;
}


export async function getHighestAlphaVersion(packageName, tag){
  const npmCmd = await spawn("npm", ["view", packageName, "versions", "--json"]);

  if (npmCmd.code !== 0) {
    throw new Error(npmCmd.stderr.toString());
  }

  const versions = JSON.parse(npmCmd.stdout.toString()) 
  const alphaVersions = versions.filter(version => version.includes(tag));
  if (alphaVersions.length === 0) {
    return 0;
  }

  alphaVersions.sort((a, b) => {
    const aVersion = a.split(tag)[0];
    const bVersion = b.split(tag)[0];
    return aVersion.localeCompare(bVersion, undefined, { numeric: true });
  });


   let list =alphaVersions[alphaVersions.length - 1].split(".");
  return list[list.length - 1];
}

getHighestAlphaVersion('vue', 'aaa').then(console.log)